package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.pojo.entity.BaseGasStation;
import com.kaili.basic.pojo.entity.BaseOil;
import com.kaili.basic.mapper.BaseOilMapper;
import com.kaili.basic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fzj
 * @since 2018-04-25
 */
@Service
public class BaseOilServiceImpl extends ServiceImpl<BaseOilMapper, BaseOil> implements IBaseOilService {

    @Autowired
    private BaseOilMapper baseOilMapper;

    @Autowired
    private IBaseGasStationService baseGasStationService;

    @Override
    public int addBaseOil(BaseOil baseOil) {
        if(baseOil.getStationName() != null && baseOil.getStationId() == null) {
            if (baseGasStationService.getBaseGasStationByName(baseOil.getStationName()) == null) {
                BaseGasStation baseGasStation = new BaseGasStation();
                baseGasStation.setStationName(baseOil.getStationName());
                baseGasStationService.addBaseGasStation(baseGasStation);
                baseOil.setStationId(baseGasStationService.getBaseGasStationByName(baseOil.getStationName()).getStationId());
            }
        }
        return baseOilMapper.addBaseOil(baseOil);
    }

    @Override
    public int updateBaseOil(BaseOil baseOil) {
        return baseOilMapper.updateBaseOil(baseOil);
    }

    @Override
    public int deleteBaseOil(BaseOil baseOil) {
        return baseOilMapper.deleteBaseOil(baseOil);
    }

    @Override
    public Page<BaseOil> getBaseOilByKeywords(Page<BaseOil> page, BaseOil baseOil) {
        List<BaseOil> baseOils = baseOilMapper.getBaseOilByKeywords(page,baseOil);
        return page.setRecords(baseOils);
    }
}
